package com.abing.starry.controller;

import cn.hutool.json.JSONUtil;
import com.abing.starry.annotation.AuthCheck;
import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.model.entity.QuestionEntity;
import com.abing.starry.model.entity.QuestionSubmitEntity;
import com.abing.starry.model.entity.UserEntity;
import com.abing.starry.model.request.question.CreateQuestionRequest;
import com.abing.starry.model.request.question.QuestionPageRequest;
import com.abing.starry.model.request.question.QuestionSubmitRequest;
import com.abing.starry.model.vo.question.QuestionEntityVO;
import com.abing.starry.service.QuestionService;
import com.abing.starry.util.TokenUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author CaptainBing
 * @Date 2024/9/5 17:33
 * @Description
 */
@RestController
@RequestMapping("/question")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService questionService;

    @GetMapping("/{questionId:.+}")
    @Operation(summary = "根据题目id查询")
    public QuestionEntity getQuestionById(@PathVariable String questionId) {
        return questionService.getById(questionId);
    }

    @PostMapping("/page")
    @Operation(summary = "管理员分页查询题目列表")
    @AuthCheck(mustRole = "admin")
    public IPage<QuestionEntity> listQuestionByPage(@RequestBody QuestionPageRequest questionPageRequest){
        return questionService.listQuestionByPage(questionPageRequest);
    }

    @PostMapping("/page/user")
    @Operation(summary = "用户查询题目列表")
    public IPage<QuestionEntityVO> listQuestionForUserByPage(@RequestBody QuestionPageRequest questionPageRequest){
        return questionService.listQuestionForUserByPage(questionPageRequest);
    }

    @PostMapping("/create")
    @Operation(summary = "创建题目")
    public boolean createQuestion(@RequestBody CreateQuestionRequest createQuestionRequest){

        QuestionEntity questionEntity = new QuestionEntity();
        BeanUtils.copyProperties(createQuestionRequest, questionEntity);
        String userId = TokenUtils.getCurrentUser().getId();
        questionEntity.setUserId(userId);
        questionEntity.setTags(JSONUtil.toJsonStr(createQuestionRequest.getTags()));
        boolean saveStatus = questionService.save(questionEntity);
        ThrowUtils.throwIf(!saveStatus, ErrorCode.OPERATION_ERROR);

        return true;

    }

    @PostMapping("/judge")
    @Operation(summary = "提交代码,进行判题")
    public QuestionSubmitEntity doQuestionSubmit(@RequestBody QuestionSubmitRequest questionSubmitRequest){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        ThrowUtils.throwIf(Objects.isNull(currentUser), ErrorCode.NOT_LOGIN_ERROR);
        questionSubmitRequest.setUserId(currentUser.getId());
        return questionService.doQuestionSubmit(questionSubmitRequest);
    }


}
